package ru.spb.osll.myfit.server;

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FilenameFilter;
import java.net.URLClassLoader;

import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;

import ru.spb.osll.myfit.server.fixtureiterator.FileFactory;

public class MyFitClassLoaderFactoryTest extends EasyMockSupport {
	private FileFactory fileFactory;
	private MyFitClassLoaderFactory classLoaderFactory;
	private File depsDir;
	private File fixturesDir;

	@Before
	public void setUp() {
		fileFactory = createStrictMock(FileFactory.class);
		depsDir = createStrictMock(File.class);
		fixturesDir = createStrictMock(File.class);
		classLoaderFactory = new MyFitClassLoaderFactory(fileFactory);
	}

	@Test
	public void testConstructor() throws Exception {
		expect(fileFactory.createFile("lib/deps")).andReturn(depsDir);
		expect(depsDir.list(isA(FilenameFilter.class))).andReturn(
				new String[] { "fitnesse.jar", "myfit.jar" });
		expect(fileFactory.createFile("lib/fixtures")).andReturn(fixturesDir);
		expect(fixturesDir.list(isA(FilenameFilter.class))).andReturn(
				new String[] { "fixtures.jar", "basefixtures.jar" });
		replayAll();
		URLClassLoader classLoader = classLoaderFactory.createClassLoader(
				"lib/deps", "lib/fixtures");
		verifyAll();
		assertEquals(4, classLoader.getURLs().length);
		assertEquals("file://localhostlib/deps" + File.separator
				+ "fitnesse.jar", classLoader.getURLs()[0].toString());
		assertEquals("file://localhostlib/deps" + File.separator + "myfit.jar",
				classLoader.getURLs()[1].toString());
		assertEquals("file://localhostlib/fixtures" + File.separator
				+ "fixtures.jar", classLoader.getURLs()[2].toString());
		assertEquals("file://localhostlib/fixtures" + File.separator
				+ "basefixtures.jar", classLoader.getURLs()[3].toString());
	}
}
